/**
 * Created with hong.rong
 * Date:2020/07/05
 */

import React, { useContext } from 'react'
import { isFunction } from 'lodash'
import { Select } from 'antd'
import { CategoryContext } from './LevelSelectContext'
import { CATEGORY_LEVEL } from '@/constant/category'

const Option = Select.Option

export default function LevelSecond({ value, onChange, ...restProps }) {
  const { levelTwo, fetchLevelData } = useContext(CategoryContext)

  const handleChange = async v => {
    isFunction(onChange) && onChange(v)
    await fetchLevelData(CATEGORY_LEVEL.THREE, v)
  }

  return (
    <Select
      placeholder="请选择"
      value={value}
      {...restProps}
      onChange={handleChange}
    >
      <Option value="">全部</Option>
      {(levelTwo || []).map(item => (
        <Option value={item.cateId}>{item.title}</Option>
      ))}
    </Select>
  )
}
